//
//  RecommendVC.m
//  BaseFramework01
//
//  Created by IMAC on 2017/6/1.
//  Copyright © 2017年 IMAC. All rights reserved.
//

#import "RecommendVC.h"
#import "SDCycleScrollView.h"
#import "CarouselModel.h"
#import "ContentModel.h"
#import "RecommendCell.h"

@interface RecommendVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView2;
@property (nonatomic, strong) UIView *scrollView;
@property (nonatomic, strong) NSMutableArray *dataArray;



@end

@implementation RecommendVC
{
    CGFloat _startY;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height - self.startY-49) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.contentInset = UIEdgeInsetsMake(SCROLLVIEWHEIGHT, 0, 0, 0);
    _tableView = tableView;
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    tableView.refreshControl = refreshControl;
    [refreshControl addTarget:self action:@selector(refreshControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:tableView];
    
    UIView *scrollView = [[UIView alloc]initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, SCROLLVIEWHEIGHT)];
    scrollView.backgroundColor = [UIColor blueColor];
    _scrollView = scrollView;
    [tableView addSubview:scrollView];
    
    
    // 网络加载 --- 创建带标题的图片轮播器
    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:scrollView.bounds delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    
    cycleScrollView2.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    [scrollView addSubview:cycleScrollView2];
    
 


    [CarouselModel requestForContentListByBaseModel:self.BaseModel Success:^(NSMutableArray *array) {
        NSMutableArray *imageURLStrings = [NSMutableArray arrayWithCapacity:array.count];
        NSMutableArray *titles = [NSMutableArray arrayWithCapacity:array.count];
        for (CarouselModel *model in array) {
            [imageURLStrings addObject:model.imageUrl];
            [titles addObject:model.contentName];
        }
        
        cycleScrollView2.titlesGroup = titles;
        //         --- 模拟加载延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            cycleScrollView2.imageURLStringsGroup = imageURLStrings;
        });
        
    } failure:^{
        
    }];
    
    [ContentModel requestForContentListBySuperModel:nil Success:^(NSMutableArray *array) {
        _dataArray = array;
        [_tableView reloadData];
    } failure:^{
        
    }];

    
  
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RecommendCell *cell = [RecommendCell cellWithTableView:tableView];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return RECOMMENDCELLHEIGHT;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y+SCROLLVIEWHEIGHT;
    
//    NSLog(@"%f",y);
    if (y<0) {
        _scrollView.transform = CGAffineTransformMakeTranslation(0, y);
        
    }
    
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
}


-(void)refreshControlAction:(UIRefreshControl *)control
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)2.0*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [control endRefreshing];
    });
}

@end
